package com.hawthorn.admin.controller.sysjob;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hawthorn.admin.service.sysjob.SysJobService;
import com.hawthorn.platform.annotation.MarkLog;
import com.hawthorn.platform.utils.common.MyUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Map;

/**
 * <p>todo 定时配置controller</p>
 * @author: andy.ten@tom.com | xukaixing@hotmail.com
 * @created: 2022/8/6 09:13
 * @version v1.0.1
 * @copyright: Copyright (c)  andyten
 */
@Api(tags = {"admin-sysjob"})
@RestController
@RequestMapping("/sysjob/")
@Slf4j
public class SysJobController
{
  @Resource
  SysJobService sysJobService;

  /**
   * <p>todo 全部任务信息</p>
   * @author: andy.ten@tom.com ｜ xukaixing@hotmail.com
   * @date: 2022/8/6 09:14
   * @version: 1.0.1
   */
  @ApiOperation(value = "查询->全部任务信息", notes = "全部任务信息")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "params", value = "params", dataTypeClass = String.class)
  })
  @PostMapping(value = "/findJobs")
  public IPage<Map<String, Object>> findJobs(@RequestBody String params)
  {
    Page<Map<String, Object>> page = new Page<>();

    String conditions = MyUtil.getConditionsWhere(params, page);
    return sysJobService.findJobs(page, conditions);
  }

  /**
   * <p>todo 查询job执行日志(指定job id)</p>
   * @author: andy.ten@tom.com ｜ xukaixing@hotmail.com
   * @date: 2022/8/9 15:10
   * @version: 1.0.1
   */
  @ApiOperation(value = "查询-> 查询job执行日志(指定job id)", notes = "查询job执行日志(指定job id)")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "params", value = "params", dataTypeClass = String.class)
  })
  @PostMapping(value = "/findJobLogsById")
  public IPage<Map<String, Object>> findJobLogsById(@RequestBody String params)
  {
    Page<Map<String, Object>> page = new Page<>();

    String conditions = MyUtil.getConditionsWhere(params, page);
    return sysJobService.findJobLogsById(page, conditions);
  }


  /**
   * <p>todo 新增job</p>
   * @author: andy.ten@tom.com ｜ xukaixing@hotmail.com
   * @date: 2022/8/9 12:52
   * @version: 1.0.1
   */
  @ApiOperation(value = "新增-> 新增job任务", notes = "新增job任务")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "param", value = "param", dataTypeClass = Map.class)
  })
  @MarkLog(value = "新增job-[{0}]", param = "jobName")
  @PostMapping(value = "/saveJob")
  public Map<String, Object> saveJob(@RequestBody Map<String, Object> param)
  {
    return sysJobService.saveJob(param);
  }

  /**
   * <p>todo 更新job</p>
   * @author: andy.ten@tom.com ｜ xukaixing@hotmail.com
   * @date: 2022/8/9 13:07
   * @version: 1.0.1
   */
  @ApiOperation(value = "更新-> 更新job", notes = "更新job")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Long.class),
      @ApiImplicitParam(name = "param", value = "param", dataTypeClass = Map.class)
  })
  @PutMapping(value = "/updateJob/{id}")
  public Map<String, Object> updateJob(@PathVariable("id") Long id, @RequestBody Map<String, Object> param)
  {
    return sysJobService.updateJob(id, param);
  }

  /**
   * <p>todo 恢复job</p>
   * @author: andy.ten@tom.com ｜ xukaixing@hotmail.com
   * @date: 2022/8/9 13:09
   * @version: 1.0.1
   */
  @ApiOperation(value = "更新-> 恢复job", notes = "恢复job")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Long.class)
  })
  @GetMapping(value = "/resumeJob")
  public void resumeJob(@RequestParam Long id)
  {
    sysJobService.resumeJob(id);
  }

  /**
   * <p>todo 暂停job</p>
   * @author: andy.ten@tom.com ｜ xukaixing@hotmail.com
   * @date: 2022/8/9 13:09
   * @version: 1.0.1
   */
  @ApiOperation(value = "更新-> 暂停job", notes = "暂停job")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Long.class)
  })
  @GetMapping(value = "/pauseJob")
  public void pauseJob(@RequestParam Long id)
  {
    sysJobService.pauseJob(id);
  }

  /**
   * <p>todo 立即运行job</p>
   * @author: andy.ten@tom.com ｜ xukaixing@hotmail.com
   * @date: 2022/8/9 13:09
   * @version: 1.0.1
   */
  @ApiOperation(value = "更新-> 立即运行job", notes = "立即运行job")
  @ApiImplicitParams({
      @ApiImplicitParam(name = "id", value = "id", dataTypeClass = Long.class)
  })
  @GetMapping(value = "/runJobNow")
  public void runJobNow(@RequestParam Long id)
  {
    sysJobService.runJobNow(id);
  }
}
